﻿using LightCAD.Core;
using LightCAD.Core.Elements;
using LightCAD.Core.Filters;
using LightCAD.Runtime;
//using netDxf.Entities;
using SkiaSharp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.ConstrainedExecution;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using static LightCAD.Core.Elements.LcPolyLine;
using LightCAD.MathLib;
using System.Reflection.Metadata;
using Avalonia.Controls;
using static LightCAD.Three.WebGLMorphtargets;
using System.Diagnostics.Metrics;
using Avalonia.Controls.Shapes;
using Avalonia.Utilities;
using System.Net;

namespace LightCAD.Drawing.Actions
{
    public class ArcAction : Curve2dAction
    {
        public static string CommandName;
        public static LcCreateMethod[] CreateMethods;
        private Vector2 startPoint;
        private Vector2 endPoint;
        private Vector2 midPoint;
        private Vector2 center;
        private Vector2 radius;
        private Vector2 length;
        private string _methodName;

        private DrawArcType drawArcType;
        public enum DrawArcType
        {
            ArcCenter = 0,
            ArcThreePoint = 1,   //三个点
        }
        static ArcAction()
        {
            CreateMethods = new LcCreateMethod[10];
            CreateMethods[0] = new LcCreateMethod()
            {
                Name = "CreateARC",
                Description = "创建弧线",
                Steps = new LcCreateStep[]
                {

                    new LcCreateStep { Name=  "Step0", Options= "ARC指定圆弧的起点或 [圆心(C)]:" },
                    new LcCreateStep { Name=  "Step1", Options= "ARC指定圆弧的第二个点或 [圆心(C)/端点(E)]:" },
                    new LcCreateStep { Name=  "Step2", Options= "ARC指定圆弧的端点:" },
                }
            };
            CreateMethods[1] = new LcCreateMethod()
            {
                Name = "PCE",
                Description = "起点,圆心,端点",
                Steps = new LcCreateStep[]
                {
                    new LcCreateStep { Name=  "Step0", Options= "ARC指定圆弧的起点或 [圆心(C)]:" },
                    new LcCreateStep { Name=  "Step1", Options= "ARC指定圆弧的圆心:" },
                    new LcCreateStep { Name=  "Step2", Options= "ARC指定圆弧的端点(按住 Ctrl 键以切换方向)或 [角度(A)/弦长(L)]:" },
                }
            };
            CreateMethods[2] = new LcCreateMethod()
            {
                Name = "PCA",
                Description = "起点,圆心,角度",
                Steps = new LcCreateStep[]
              {
                    new LcCreateStep { Name=  "Step0", Options= "ARC指定圆弧的起点或 [圆心(C)]:" },
                    new LcCreateStep { Name=  "Step1", Options= "ARC指定圆弧的圆心:" },
                    new LcCreateStep { Name=  "Step2", Options= "ARC指定夹角(按住 Ctrl 键以切换方向):" },
              }
            };
            CreateMethods[3] = new LcCreateMethod()
            {
                Name = "PCL",
                Description = "起点,圆心,长度",
                Steps = new LcCreateStep[]
           {
                    new LcCreateStep { Name=  "Step0", Options= "ARC指定圆弧的起点或 [圆心(C)]:" },
                    new LcCreateStep { Name=  "Step1", Options= "ARC指定圆弧的圆心:" },
                    new LcCreateStep { Name=  "Step2", Options= "ARC指定弦长(按住 Ctrl 键以切换方向):" },
           }
            };
            CreateMethods[4] = new LcCreateMethod()
            {
                Name = "PEA",
                Description = "起点,端点,角度",
                Steps = new LcCreateStep[]
                {
                    new LcCreateStep { Name=  "Step0", Options= "ARC指定圆弧的起点或 [圆心(C)]:" },
                    new LcCreateStep { Name=  "Step1", Options= "ARC指定圆弧的端点:" },
                    new LcCreateStep { Name=  "Step2", Options= "ARC指定夹角(按住 Ctrl 键以切换方向):" },
                }
            };
            CreateMethods[5] = new LcCreateMethod()
            {
                Name = "PED",
                Description = "起点,端点,方向",
                Steps = new LcCreateStep[]
               {
                    new LcCreateStep { Name=  "Step0", Options= "ARC指定圆弧的起点或 [圆心(C)]:" },
                    new LcCreateStep { Name=  "Step1", Options= "ARC指定圆弧的端点:" },
                    new LcCreateStep { Name=  "Step2", Options= "ARC指定圆弧起点的相切方向(按住 Ctrl 键以切换方向):" },
               }
            };
            CreateMethods[6] = new LcCreateMethod()
            {
                Name = "PER",
                Description = "起点,端点,半径",
                Steps = new LcCreateStep[]
            {
                    new LcCreateStep { Name=  "Step0", Options= "ARC指定圆弧的起点或 [圆心(C)]:" },
                    new LcCreateStep { Name=  "Step1", Options= "ARC指定圆弧的端点:" },
                    new LcCreateStep { Name=  "Step2", Options= "ARC指定圆弧起点的半径(按住 Ctrl 键以切换方向):" },
            }
            };
            CreateMethods[7] = new LcCreateMethod()
            {
                Name = "CPE",
                Description = "圆心,起点,端点",
                Steps = new LcCreateStep[]
              {
                    new LcCreateStep { Name=  "Step0", Options= "ARC指定圆弧的圆心:" },
                    new LcCreateStep { Name=  "Step1", Options= "ARC指定圆弧的起点:" },
                    new LcCreateStep { Name=  "Step2", Options= "ARC指定圆弧的端点(按住 Ctrl 键以切换方向)或 [角度(A)/弦长(L)]:" },
              }
            };
            CreateMethods[8] = new LcCreateMethod()
            {
                Name = "CPA",
                Description = "圆心,起点,角度",
                Steps = new LcCreateStep[]
              {
                    new LcCreateStep { Name=  "Step0", Options= "ARC指定圆弧的圆心:" },
                    new LcCreateStep { Name=  "Step1", Options= "ARC指定圆弧的起点:" },
                    new LcCreateStep { Name=  "Step2", Options= "ARC指定夹角(按住 Ctrl 键以切换方向):" },
              }
            };
            CreateMethods[9] = new LcCreateMethod()
            {
                Name = "CPL",
                Description = "圆心,起点,长度",
                Steps = new LcCreateStep[]
              {
                    new LcCreateStep { Name=  "Step0", Options= "ARC指定圆弧的圆心:" },
                    new LcCreateStep { Name=  "Step1", Options= "ARC指定圆弧的起点:" },
                    new LcCreateStep { Name=  "Step2", Options= "ARC指定弦长(按住 Ctrl 键以切换方向):" },
              }
            };
        }
        internal static void Initilize()
        {
            ElementActions.Arc = new ArcAction();
            LcDocument.ElementActions.Add(BuiltinElementType.Arc, ElementActions.Arc);
        }
        private InElementsInputer InElementsInputer { get; set; }
        private ElementSetInputer ElementInputers { get; set; }
        private ElementInputer ElementInputer { get; set; }
        private CmdTextInputer CmdTextInputer { get; set; }
        private PointInputer PointInputer { get; set; }
        private ArcAction() { }
        public ArcAction(IDocumentEditor docEditor) : base(docEditor)
        {
            this.commandCtrl.WriteInfo("命令：ARC");
        }
        private LcCreateMethod GetMethod(string method)
        {
            if (method == null) return CreateMethods[0];
            var getted = CreateMethods.FirstOrDefault((m) => m.Name == method);
            if (getted == null)
                return CreateMethods[0];
            else
                return getted;
        }

        public async void ExecCreate(string[] args = null)
        {
            var method = "CreateARC";
            if (args != null && args.Length > 0)
            {
                method = args[0];
            }
            var curMethod = this.GetMethod(method);
            _methodName = curMethod.Name;
            this.PointInputer = new PointInputer(this.docEditor);
            this.StartCreating();
            if (curMethod.Name == "PCE") goto Method_PCE_Step0;
            if (curMethod.Name == "PCA") goto Method_PCA_Step0;
            if (curMethod.Name == "PCL") goto Method_PCL_Step0;
            if (curMethod.Name == "PEA") goto Method_PEA_Step0;

            if (curMethod.Name == "CPE") goto Method_CPE_Step0;
            if (curMethod.Name == "CPA") goto Method_CPA_Step0;
            if (curMethod.Name == "CPL") goto Method_CPL_Step0;
            #region  CreateARC
            Method_CreateARC_Step0:

            curMethod = this.SetCurMethod(CreateMethods, 0);
            var step0 = this.SetCurStep(curMethod, 0);
            curMethod = this.GetMethod("CreateARC");
            _methodName = curMethod.Name;

            var result0 = await PointInputer.Execute(step0.Options);
            if (PointInputer.isCancelled) { this.Cancel(); return; }
            // zcb: 增加Res0为空的判定
            if (result0 == null)
            {
                this.Cancel();
                goto End;
            }
            if (result0.ValueX == null)
            {
                if (result0.Option != null)
                {
                    //TODO:AutoCAD画线输入一个数字，是怎么确定点的？
                    if (result0.Option == "C")
                    {

                    }
                }
                else
                    goto Method_CreateARC_Step1;
            }
            else
            {
                this.startPoint = (Vector2)result0.ValueX;
                goto Method_CreateARC_Step1;
            }

        //ARC指定圆弧的第二个点或 [圆心(C)/端点(E)]: 
        Method_CreateARC_Step1:
            this.drawArcType = DrawArcType.ArcThreePoint;  //三点画弧
            var step1 = this.SetCurStep(curMethod, 1);
            var result1 = await PointInputer.Execute(step1.Options);
            if (PointInputer.isCancelled)
            {
                this.Cancel();
                return;
            }
            if (result1.ValueX == null)
            {
                if (result1.Option != null)
                {
                    //goto Step1;
                    if (result1.Option == "C")
                    {
                        goto Method_PCE_Step1;
                    }
                    if (result1.Option == "E")
                    {

                    }
                }

                else
                    goto Method_CreateARC_Step2;
            }
            else
            {
                this.midPoint = (Vector2)result1.ValueX;


                goto Method_CreateARC_Step2;
            }
        //    ARC指定圆弧的端点:
        Method_CreateARC_Step2:
            var step2 = this.SetCurStep(curMethod, 2);
            var result2 = await PointInputer.Execute(step2.Options);
            if (PointInputer.isCancelled)
            {
                this.Cancel();
                goto End;
            }
            if (result2.ValueX == null)
            {
            }
            else
            {

                this.endPoint = (Vector2)result2.ValueX;
                //if (this.endPoint == this.midPoint)
                //    goto Step2;
                //if (this.endPoint == this.center)
                //    goto Step2;

                CreateArc();
                goto End;
            }
        #endregion

        #region   PCE起点,圆心,端点
        Method_PCE_Step0:
            curMethod = this.SetCurMethod(CreateMethods, 1);
            var pce_step0 = this.SetCurStep(curMethod, 0);
            curMethod = this.GetMethod("PCE");
            _methodName = curMethod.Name;
            var pce_result0 = await PointInputer.Execute(pce_step0.Options);
            if (PointInputer.isCancelled) { this.Cancel(); return; }
            // zcb: 增加Res0为空的判定
            if (pce_result0 == null)
            {
                this.Cancel();
                goto End;
            }
            if (pce_result0.ValueX == null)
            {
                if (pce_result0.Option != null)
                {
                    //TODO:AutoCAD画线输入一个数字，是怎么确定点的？
                    if (pce_result0.Option == "C")
                    {

                    }
                }
                else
                    goto Method_PCE_Step1;
            }
            else
            {
                this.startPoint = (Vector2)pce_result0.ValueX;
                goto Method_PCE_Step1;
            }
        Method_PCE_Step1:
            var pce_step1 = this.SetCurStep(curMethod, 1);
            var pce_result1 = await PointInputer.Execute(pce_step1.Options);
            if (PointInputer.isCancelled)
            {
                this.Cancel();
                goto End;
            }
            if (pce_result1.ValueX == null) { }
            else
            {
                this.center = (Vector2)pce_result1.ValueX;

                goto Method_PCE_Step2;
            }
        Method_PCE_Step2:
            var pce_step2 = this.SetCurStep(curMethod, 2);
            var pce_result2 = await PointInputer.Execute(pce_step2.Options);
            if (PointInputer.isCancelled)
            {
                this.Cancel();
                goto End;
            }
            if (pce_result2.ValueX == null)
            {
            }
            else
            {
                this.endPoint = (Vector2)pce_result2.ValueX;
                CreateArc();
                goto End;
            }
        #endregion

        #region  PCA 起点，圆心，角度
        Method_PCA_Step0:
            curMethod = this.SetCurMethod(CreateMethods, 2);
            var pca_step0 = this.SetCurStep(curMethod, 0);
            curMethod = this.GetMethod("PCA");
            _methodName = curMethod.Name;
            var pca_result0 = await PointInputer.Execute(pca_step0.Options);
            if (PointInputer.isCancelled) { this.Cancel(); return; }
            // zcb: 增加Res0为空的判定
            if (pca_result0 == null)
            {
                this.Cancel();
                goto End;
            }
            if (pca_result0.ValueX == null)
            {
                if (pca_result0.Option != null)
                {
                    //TODO:AutoCAD画线输入一个数字，是怎么确定点的？
                    if (pca_result0.Option == "C")
                    {

                    }
                }
                else
                    goto Method_PCA_Step1;
            }
            else
            {
                this.startPoint = (Vector2)pca_result0.ValueX;
                goto Method_PCA_Step1;
            }
        Method_PCA_Step1:
            var pca_step1 = this.SetCurStep(curMethod, 1);
            var pca_result1 = await PointInputer.Execute(pca_step1.Options);
            if (PointInputer.isCancelled)
            {
                this.Cancel();
                goto End;
            }
            if (pca_result1.ValueX == null) { }
            else
            {
                this.center = (Vector2)pca_result1.ValueX;

                goto Method_PCA_Step2;
            }
        Method_PCA_Step2:
            var pca_step2 = this.SetCurStep(curMethod, 2);
            var pca_result2 = await PointInputer.Execute(pca_step2.Options);
            double startangle, endangle;
            double angle;
            if (PointInputer.isCancelled)
            {
                this.Cancel();
                goto End;
            }
            if (pca_result2.ValueX == null)
            {
                //如果输入的是角度，应该如何画圆弧
            }
            else
            {
                this.endPoint = (Vector2)pca_result2.ValueX;
                CreateArc();
                goto End;

            }
        #endregion

        #region PCL起点，圆心，长度
        Method_PCL_Step0:
            curMethod = this.SetCurMethod(CreateMethods, 3);
            var pcl_step0 = this.SetCurStep(curMethod, 0);
            curMethod = this.GetMethod("PCL");
            _methodName = curMethod.Name;
            var pcl_result0 = await PointInputer.Execute(pcl_step0.Options);
            if (PointInputer.isCancelled) { this.Cancel(); return; }
            // zcb: 增加Res0为空的判定
            if (pcl_result0 == null)
            {
                this.Cancel();
                goto End;
            }
            if (pcl_result0.ValueX == null)
            {
                if (pcl_result0.Option != null)
                {
                    //TODO:AutoCAD画线输入一个数字，是怎么确定点的？
                    if (pcl_result0.Option == "C")
                    {

                    }
                }
                else
                    goto Method_PCL_Step1;
            }
            else
            {
                this.startPoint = (Vector2)pcl_result0.ValueX;
                goto Method_PCL_Step1;
            }
        Method_PCL_Step1:
            var pcl_step1 = this.SetCurStep(curMethod, 1);
            var pcl_result1 = await PointInputer.Execute(pcl_step1.Options);
            if (PointInputer.isCancelled)
            {
                this.Cancel();
                goto End;
            }
            if (pcl_result1.ValueX == null) { }
            else
            {
                this.center = (Vector2)pcl_result1.ValueX;
                goto Method_PCL_Step2;
            }
        Method_PCL_Step2:
            var pcl_step2 = this.SetCurStep(curMethod, 2);
            var pcl_result2 = await PointInputer.Execute(pcl_step2.Options);
            if (PointInputer.isCancelled)
            {
                this.Cancel();
                goto End;
            }
            if (pcl_result2.ValueX == null)
            {
                //如果输入的是角度，应该如何画圆弧
            }
            else
            {
                this.endPoint = (Vector2)pcl_result2.ValueX;
                CreateArc();
                goto End;

            }
        #endregion

        #region  PEA 起点，端点，角度
        Method_PEA_Step0:
            curMethod = this.SetCurMethod(CreateMethods, 4);
            var pea_step0 = this.SetCurStep(curMethod, 0);
            curMethod = this.GetMethod("PEA");
            _methodName = curMethod.Name;
            var pea_result0 = await PointInputer.Execute(pea_step0.Options);
            if (PointInputer.isCancelled) { this.Cancel(); return; }
            // zcb: 增加Res0为空的判定
            if (pea_result0 == null)
            {
                this.Cancel();
                goto End;
            }
            if (pea_result0.ValueX == null)
            {
                if (pea_result0.Option != null)
                {
                    //TODO:AutoCAD画线输入一个数字，是怎么确定点的？
                    if (pea_result0.Option == "C")
                    {

                    }
                }
                else
                    goto Method_PEA_Step1;
            }
            else
            {
                this.startPoint = (Vector2)pea_result0.ValueX;
                goto Method_PEA_Step1;
            }
        Method_PEA_Step1:
            var pea_step1 = this.SetCurStep(curMethod, 1);
            var pea_result1 = await PointInputer.Execute(pea_step1.Options);
            if (PointInputer.isCancelled)
            {
                this.Cancel();
                goto End;
            }
            if (pea_result1.ValueX == null)
            {
                
            }
            else
            {
                this.midPoint = (Vector2)pea_result1.ValueX;
                goto Method_PEA_Step2;
            }
        Method_PEA_Step2:
            var pea_step2 = this.SetCurStep(curMethod, 2);
            var pea_result2 = await PointInputer.Execute(pea_step2.Options);
            if (PointInputer.isCancelled)
            {
                this.Cancel();
                goto End;
            }
            if (pea_result2.ValueX == null)
            {

            }
            else
            {
                this.endPoint = (Vector2)pea_result2.ValueX;
                
                CreateArc();
                goto End;
            }

        #endregion

        #region   CPE 圆心，起点，端点
        Method_CPE_Step0:
            curMethod = this.SetCurMethod(CreateMethods, 7);
            var cpe_step0 = this.SetCurStep(curMethod, 0);
            curMethod = this.GetMethod("CPE");
            _methodName = curMethod.Name;
            var cpe_result0 = await PointInputer.Execute(cpe_step0.Options);
            if (PointInputer.isCancelled) { this.Cancel(); return; }
            // zcb: 增加Res0为空的判定
            if (cpe_result0 == null)
            {
                this.Cancel();
                goto End;
            }
            if (cpe_result0.ValueX == null)
            {
                if (cpe_result0.Option != null)
                {
                    //TODO:AutoCAD画线输入一个数字，是怎么确定点的？
                }
                else
                    goto Method_CPE_Step1;
            }
            else
            {
                this.center = (Vector2)cpe_result0.ValueX;
                goto Method_CPE_Step1;
            }
         Method_CPE_Step1:
            var cpe_step1 = this.SetCurStep(curMethod, 1);
            curMethod = this.GetMethod("CPE");
            _methodName = curMethod.Name;
            var cpe_result1 = await PointInputer.Execute(cpe_step1.Options);
            if (PointInputer.isCancelled) { this.Cancel(); return; }
            // zcb: 增加Res0为空的判定
            if (cpe_result1 == null)
            {
                this.Cancel();
                goto End;
            }
            if (cpe_result1.ValueX == null)
            {
                if (cpe_result1.Option != null)
                {
                    //TODO:AutoCAD画线输入一个数字，是怎么确定点的？
                }
                else
                    goto Method_CPE_Step2;
            }
            else
            {
                this.startPoint = (Vector2)cpe_result1.ValueX;
                goto Method_CPE_Step2;
            }
        Method_CPE_Step2:
            var cpe_step2 = this.SetCurStep(curMethod, 2);
            var cpe_result2 = await PointInputer.Execute(cpe_step2.Options);
            if (PointInputer.isCancelled)
            {
                this.Cancel();
                goto End;
            }
            if (cpe_result2.ValueX == null)
            {

            }
            else
            {
                this.endPoint = (Vector2)cpe_result2.ValueX;
                CreateArc();
                goto End;
            }
        #endregion
 
        #region  CPA圆心，起点，角度
        Method_CPA_Step0:
            curMethod = this.SetCurMethod(CreateMethods, 8);
            var cpa_step0 = this.SetCurStep(curMethod, 0);
            curMethod = this.GetMethod("CPA");
            _methodName = curMethod.Name;
            var cpa_result0 = await PointInputer.Execute(cpa_step0.Options);
            if (PointInputer.isCancelled) { this.Cancel(); return; }
            // zcb: 增加Res0为空的判定
            if (cpa_result0 == null)
            {
                this.Cancel();
                goto End;
            }
            if (cpa_result0.ValueX == null)
            {
                if (cpa_result0.Option != null)
                {
                    //TODO:AutoCAD画线输入一个数字，是怎么确定点的？
                }
                else
                    goto Method_CPA_Step1;
            }
            else
            {
                this.center = (Vector2)cpa_result0.ValueX;
                goto Method_CPA_Step1;
            }
            Method_CPA_Step1:
            var cpa_step1 = this.SetCurStep(curMethod, 1);
            curMethod = this.GetMethod("CPA");
            _methodName = curMethod.Name;
            var cpa_result1 = await PointInputer.Execute(cpa_step1.Options);
            if (PointInputer.isCancelled) { this.Cancel(); return; }
            // zcb: 增加Res0为空的判定
            if (cpa_result1 == null)
            {
                this.Cancel();
                goto End;
            }
            if (cpa_result1.ValueX == null)
            {
                if (cpa_result1.Option != null)
                {
                    //TODO:AutoCAD画线输入一个数字，是怎么确定点的？
                }
                else
                    goto Method_CPA_Step2;
            }
            else
            {
                this.startPoint = (Vector2)cpa_result1.ValueX;
                goto Method_CPA_Step2;
            }
        Method_CPA_Step2:
            var cpa_step2 = this.SetCurStep(curMethod, 2);
            var cpa_result2 = await PointInputer.Execute(cpa_step2.Options);
            if (PointInputer.isCancelled)
            {
                this.Cancel();
                goto End;
            }
            if (cpa_result2.ValueX == null)
            {

            }
            else
            {
                this.endPoint = (Vector2)cpa_result2.ValueX;

                CreateArc();
                goto End;
            }

        #endregion

        #region  CPL圆心，起点，长度
        Method_CPL_Step0:
            curMethod = this.SetCurMethod(CreateMethods, 9);
            var cpl_step0 = this.SetCurStep(curMethod, 0);
            curMethod = this.GetMethod("CPL");
            _methodName = curMethod.Name;
            var cpl_result0 = await PointInputer.Execute(cpl_step0.Options);
            if (PointInputer.isCancelled) { this.Cancel(); return; }
            // zcb: 增加Res0为空的判定
            if (cpl_result0 == null)
            {
                this.Cancel();
                goto End;
            }
            if (cpl_result0.ValueX == null)
            {
                if (cpl_result0.Option != null)
                {
                    //TODO:AutoCAD画线输入一个数字，是怎么确定点的？
                }
                else
                    goto Method_CPL_Step1;
            }
            else
            {
                this.center = (Vector2)cpl_result0.ValueX;
                goto Method_CPL_Step1;
            }
        Method_CPL_Step1:
            var cpl_step1 = this.SetCurStep(curMethod, 1);
            var cpl_result1 = await PointInputer.Execute(cpl_step1.Options);
            if (PointInputer.isCancelled) { this.Cancel(); return; }
            // zcb: 增加Res0为空的判定
            if (cpl_result1 == null)
            {
                this.Cancel();
                goto End;
            }
            if (cpl_result1.ValueX == null)
            {
                if (cpl_result1.Option != null)
                {
                    //TODO:AutoCAD画线输入一个数字，是怎么确定点的？
                }
                else
                    goto Method_CPL_Step2;
            }
            else
            {
                this.startPoint = (Vector2)cpl_result1.ValueX;
                goto Method_CPL_Step2;
            }
        Method_CPL_Step2:
            var cpl_step2 = this.SetCurStep(curMethod, 2);
            var cpl_result2 = await PointInputer.Execute(cpl_step2.Options);
            if (PointInputer.isCancelled)
            {
                this.Cancel();
                goto End;
            }
            if (cpl_result2.ValueX == null)
            {
                //如果输入的是角度，应该如何画圆弧
            }
            else
            {
                this.endPoint = (Vector2)cpl_result2.ValueX;
                CreateArc();
                goto End;
            }
        #endregion

        //注释掉 by:Zcb 2023-07-21 21:42
        End:
            this.EndCreating();
        }
        public Arc2d CreateARC1(Vector2 sp, Vector2 mp, Vector2 ep)
        {
            Arc2d arc2D = new Arc2d();
            arc2D.Center = LcArc.GetCenter(sp, mp, ep);
            this.center = arc2D.Center;
            arc2D.Radius = Vector2.Distance(sp, arc2D.Center);
            double Degrees, smidAngle, emidAngle;
            arc2D.StartAngle = GetDegreesByTwoLine(new(arc2D.Center.X, arc2D.Center.Y), new(arc2D.Center.X + arc2D.Radius, arc2D.Center.Y), new(arc2D.Center.X, arc2D.Center.Y), new(sp.X, sp.Y)); //计算圆弧的起始角度
            arc2D.EndAngle = GetDegreesByTwoLine(new(arc2D.Center.X, arc2D.Center.Y), new(arc2D.Center.X + arc2D.Radius, arc2D.Center.Y), new(arc2D.Center.X, arc2D.Center.Y), new(ep.X, ep.Y));  //计算圆弧的结束角度
            smidAngle = GetDegreesByTwoLine(new(arc2D.Center.X, arc2D.Center.Y), new(sp.X, sp.Y), new(arc2D.Center.X, arc2D.Center.Y), new(mp.X, mp.Y));  //起始点到中点之间的角度
            emidAngle = GetDegreesByTwoLine(new(arc2D.Center.X, arc2D.Center.Y), new(ep.X, ep.Y), new(arc2D.Center.X, arc2D.Center.Y), new(mp.X, mp.Y));
            if (IsTopInLine(arc2D.Center, sp, new(arc2D.Center.X + arc2D.Radius, arc2D.Center.Y)) < 0)
            {
                arc2D.StartAngle = 360 - arc2D.StartAngle;
            }
            if (IsTopInLine(arc2D.Center, ep, new(arc2D.Center.X + arc2D.Radius, arc2D.Center.Y)) < 0)
            {

                arc2D.EndAngle = 360 - arc2D.EndAngle;
            }
            double moveAngle = smidAngle + emidAngle;
            double xAngle = arc2D.EndAngle - arc2D.StartAngle;
            if (xAngle < 0)
            {
                xAngle += 360;
            }
            if (xAngle - 0.1 < moveAngle && moveAngle < xAngle + 0.1)
            {
                arc2D.StartAngle = arc2D.StartAngle / 180 * Math.PI;
                arc2D.EndAngle = arc2D.EndAngle / 180 * Math.PI;

            }
            else
            {
                Degrees = arc2D.StartAngle;
                arc2D.StartAngle = arc2D.EndAngle / 180 * Math.PI;
                arc2D.EndAngle = Degrees / 180 * Math.PI;
            }
            return arc2D;
        }
      
        public Arc2d CreatePCE(Vector2 sp,Vector2 cp,Vector2 ep)
        {
            Arc2d arc2D = new Arc2d();
            arc2D.Center = cp;
            arc2D.StartAngle = (Math.Atan2(sp.Y - cp.Y, sp.X - cp.X) * 180 / Math.PI);
            arc2D.EndAngle = (Math.Atan2(ep.Y - cp.Y, ep.X - cp.X) * 180 / Math.PI);
            arc2D.Radius = Vector2.Distance(center, sp);
            if (arc2D.StartAngle < 0)
            {
                arc2D.StartAngle += 360;
            }
            if (arc2D.EndAngle < 0)
            {
                arc2D.EndAngle += 360;
            }
            arc2D.StartAngle = arc2D.StartAngle / 180 * Math.PI;
            arc2D.EndAngle = arc2D.EndAngle / 180 * Math.PI;
            return arc2D;
        }
        public Arc2d CreatePCA(Vector2 sp, Vector2 cp, Vector2 ap)  //起点，圆心，角度的方法
        {
            Arc2d arc2D = new Arc2d();
            arc2D.Center = cp;
            arc2D.StartAngle = (Math.Atan2(sp.Y - cp.Y, sp.X - cp.X) * 180 / Math.PI);
            double moveAngle = (Math.Atan2(ap.Y - cp.Y, ap.X - cp.X) * 180 / Math.PI);
            arc2D.Radius = Vector2.Distance(center, sp);
            arc2D.EndAngle = arc2D.StartAngle + moveAngle; //端点角度
            if (arc2D.EndAngle > 360)
            {
                arc2D.EndAngle = arc2D.EndAngle - 360;
            }
            arc2D.EndAngle = arc2D.EndAngle / 180 * Math.PI;
            arc2D.StartAngle = arc2D.StartAngle / 180 * Math.PI;
            return arc2D;

        }
        public Arc2d CreatePCL(Vector2 sp, Vector2 cp, Vector2 ap)
        {
            Arc2d arc2D = new Arc2d();

            arc2D.Center = cp;
            arc2D.Radius = Vector2.Distance(cp, sp);//计算半径
            double chordLength = Vector2.Distance(sp, ap); //计算弦长
            double chordAngleRadians = (2 * Math.Asin(chordLength / (2 * arc2D.Radius))) * 180 / Math.PI;  //计算弦角度

            arc2D.StartAngle = (Math.Atan2(sp.Y - cp.Y, sp.X - cp.X) * 180 / Math.PI);
            if (arc2D.StartAngle < 0)
            {
                arc2D.StartAngle += 360;
            }
            arc2D.EndAngle = (arc2D.StartAngle + chordAngleRadians) % 360;
            arc2D.StartAngle = arc2D.StartAngle / 180 * Math.PI;
            arc2D.EndAngle = arc2D.EndAngle / 180 * Math.PI;
            if (arc2D.EndAngle < 0)
            {
                arc2D.EndAngle += 360;
            }
            return arc2D;
        }
        public Arc2d CreatePEA(Vector2 sp, Vector2 ep, Vector2 eps) //ep为midpoint但是是端点
        {
            Arc2d arc2D = new Arc2d();
            double angle = (Math.Atan2(eps.Y-sp.Y, eps.X-sp.X) * 180 / Math.PI); //圆心旋转角度，弦长角度
            if (angle < 0)
            {
                angle += 360;
            }
            double length = Vector2.Distance(sp, ep);  //起点和终点之间的距离 也就是弦长
            arc2D.Radius = (length / 2) / Math.Sin((angle / 2) * Math.PI / 180);
            arc2D.Center = new Vector2();
            arc2D.Center.X = sp.X + arc2D.Radius * Math.Cos((angle / 2) * Math.PI / 180); //圆心的x轴
            arc2D.Center.Y = sp.Y + arc2D.Radius * Math.Sin((angle / 2) * Math.PI / 180);
            center = arc2D.Center;
            if (angle > 180)
            {
                ep = sp;   //将开始点与端点取反
                sp = ep;
            }
            arc2D.StartAngle = (Math.Atan2(sp.Y - arc2D.Center.Y, sp.X - arc2D.Center.X) * 180 / Math.PI);
            arc2D.EndAngle = arc2D.StartAngle + angle;
            if (arc2D.EndAngle > 360)
            {
                arc2D.EndAngle -= 360;
            }
            arc2D.StartAngle = arc2D.StartAngle * Math.PI / 180;
            arc2D.EndAngle = arc2D.EndAngle * Math.PI / 180;
            return arc2D;
        }

        #region   创建圆弧的代码
        private bool CreateArc()
        {
            Arc2d arc2D = new Arc2d();
            var doc = this.docRt.Document;
            DocumentManager.CurrentRecorder.BeginAction("arc");
            var lcArc = doc.CreateObject<LcArc>();
            if (_methodName == "CreateARC")
            {
                arc2D = CreateARC1(this.startPoint,this.midPoint, this.endPoint);
            }
            if (_methodName == "PCE" || _methodName == "CPE")
            {
                arc2D = CreatePCE(this.startPoint, center, this.endPoint);
            }
            if (_methodName == "PCA" || _methodName == "CPA")
            {
                arc2D = CreatePCA(this.startPoint, center, this.endPoint);
            }
            if (_methodName == "PCL" || _methodName == "CPL")
            {
                arc2D = CreatePCL(this.startPoint,center,this.endPoint);
            }

            if(_methodName == "PEA")
            {
                arc2D = CreatePEA(this.startPoint, this.midPoint, this.endPoint);
            }

            lcArc.Startp = this.startPoint;
            this.endPoint.X = center.X + arc2D.Radius * Math.Cos(arc2D.EndAngle);  //求端点
            this.endPoint.Y = center.Y + arc2D.Radius * Math.Sin(arc2D.EndAngle);
            lcArc.Endp = this.endPoint;
            lcArc.Center = arc2D.Center;
            lcArc.StartAngle = arc2D.StartAngle;
            lcArc.EndAngle = arc2D.EndAngle;
            lcArc.Radius = arc2D.Radius;
            lcArc.Midp = this.Getmidpoint(arc2D);  //获取中间点
            doc.ModelSpace.InsertElement(lcArc);
            this.docRt.Action.ClearSelects();
            DocumentManager.CurrentRecorder.EndAction();
            return true;
        }
        #endregion
        public override void Cancel()
        {
            base.Cancel();
            this.vportRt.SetCreateDrawer(null);
            //TODO:
            this.EndCreating();
        }
        private void test()
        {
        }

        public override void Draw(SKCanvas canvas, LcElement element, Vector2 offset)
        {
            Matrix3 matrix = Matrix3.GetTranslate(offset);
            var grp = element as LcArc;
            Arc2d arc2D = new Arc2d();  //逻辑模型
            arc2D.IsClockwise = false;
            arc2D.StartAngle = grp.StartAngle;
            arc2D.EndAngle = grp.EndAngle;
            arc2D.Center = grp.Center;
            arc2D.Radius = grp.Radius;
            using (var elePen = new SKPaint { Color = new SKColor(element.GetColorValue()), IsStroke = true })
            {
                this.vportRt.DrawArc(arc2D, matrix, canvas, elePen);
            }
            var box = grp.BoundingBox;
            DrawAuxLine(canvas, box.LeftBottom, box.RightBottom);
            DrawAuxLine(canvas, box.RightBottom, box.RightTop);
            DrawAuxLine(canvas, box.RightTop, box.LeftTop);
            DrawAuxLine(canvas, box.LeftTop, box.LeftBottom);

            return;
        }

        public override void DrawAuxLines(SKCanvas canvas)
        {
            var mp = this.vportRt.PointerMovedPosition.ToVector2d();
            var wcs_mp = this.vportRt.ConvertScrToWcs(mp);
            if(_methodName == "CreateARC")
            {
                if(this.startPoint != null && this.midPoint == null)
                {
                    DrawAuxLine(canvas, this.startPoint, wcs_mp);
                }
                if(this.startPoint != null && this.midPoint != null)
                {
                    DrawAuxArc(canvas, wcs_mp);
                }
            }
            if (_methodName == "PCE"|| _methodName == "PCA")
            {
                if (this.startPoint != null && this.center == null)
                {
                    DrawAuxLine(canvas, this.startPoint, wcs_mp);
                }
                if(this.startPoint != null && this.center != null)
                {
                    DrawAuxLine(canvas, this.center, wcs_mp);
                    DrawAuxArc(canvas, wcs_mp);
                }
            }
            if(_methodName == "PCL")
            {
                if (this.startPoint != null && this.center == null)
                {
                    DrawAuxLine(canvas, this.startPoint, wcs_mp);
                }
                if (this.startPoint != null && this.center != null)
                {
                    DrawAuxLine(canvas, this.startPoint, wcs_mp);
                    DrawAuxArc(canvas, wcs_mp);
                }
            }
            if (_methodName == "PEA")
            {
                if (this.startPoint != null && this.midPoint == null)
                {
                    DrawAuxLine(canvas, this.startPoint, wcs_mp);
                }
                if (this.startPoint != null && this.midPoint != null)
                {
                    DrawAuxLine(canvas, this.startPoint, wcs_mp);
                    DrawAuxArc(canvas, wcs_mp);
                }

            }
            if(_methodName == "CPE" || _methodName == "CPA")
            {
                if (center != null && startPoint == null)
                {
                    DrawAuxLine(canvas, center, wcs_mp);
                }
                if(center !=null &&startPoint != null)
                {
                    DrawAuxLine(canvas, center, wcs_mp);
                    DrawAuxArc(canvas, wcs_mp);
                }
            }
            if (_methodName == "CPL")
            {
                if (center != null && startPoint == null)
                {
                    DrawAuxLine(canvas, center, wcs_mp);
                }
                if(center != null && startPoint != null)
                {
                    DrawAuxLine(canvas, startPoint, wcs_mp);
                    DrawAuxArc(canvas, wcs_mp);
                }
            }
        }
        private void DrawArcBy3P(SKCanvas canvas, Vector2 p0)
        {
            LcArc lcarc = new LcArc();
            lcarc.Startp = this.startPoint;
            lcarc.Midp = this.midPoint;
            lcarc.Endp = p0;
            lcarc.LoadArcStatsProperty();
            var center = this.vportRt.ConvertWcsToScr(lcarc.Center).ToSKPoint();
            var start = this.vportRt.ConvertWcsToScr(this.startPoint).ToSKPoint();
            var mid = this.vportRt.ConvertWcsToScr(this.midPoint).ToSKPoint();
            var end = this.vportRt.ConvertWcsToScr(p0).ToSKPoint();
            if (mid == end) return;
            //辅助参考线
            //canvas.DrawLine(start, center, Constants.auxElementPen);
            //canvas.DrawLine(end, center, Constants.auxElementPen);
            double radius = Math.Sqrt(Math.Pow(center.X - end.X, 2) + Math.Pow(center.Y - end.Y, 2));
            float x1 = (float)(center.X - radius);
            float y1 = (float)(center.Y - radius);
            float x2 = (float)(center.X + radius);
            float y2 = (float)(center.Y + radius);
            SKRect skrect = new SKRect(x1, y1, x2, y2);
            canvas.DrawArc(skrect, (float)lcarc.StartAngle, (float)lcarc.MoveAngle, false, Constants.auxElementPen);
        }
        private void DrawArcByCenterAnd2P(SKCanvas canvas, Vector2 p0)
        {
            var startAngle = (Math.Atan2(this.startPoint.Y - center.Y, this.startPoint.X - center.X) * 180 / Math.PI);
            var endAngle = (Math.Atan2(p0.Y - center.Y, p0.X - center.X) * 180 / Math.PI);
            var radius = Vector2.Distance(center, this.startPoint);
            if (startAngle < 0)
            {
                startAngle += 360;
            }
            if (endAngle < 0)
            {
                endAngle += 360;
            }
            endAngle = endAngle / 180 * Math.PI;
            startAngle = startAngle / 180 * Math.PI;
            Matrix3 matrix = Matrix3.GetTranslate(new Vector2(0, 0));
            Arc2d arc2D = new Arc2d();  //逻辑模型
            arc2D.IsClockwise = false;
            arc2D.StartAngle = startAngle;
            arc2D.EndAngle = endAngle;
            arc2D.Center = center;
            arc2D.Radius = radius;
            this.vportRt.DrawArc(arc2D, matrix, canvas, Constants.auxElementPen);
            return;
        }
        private void DrawArcByCenterAndLength(SKCanvas canvas, Vector2 p0)
        {
            var radius = Vector2.Distance(center, this.startPoint);//计算半径
            double chordLength = Vector2.Distance(this.startPoint, p0); //计算弦长
            double chordAngleRadians = (2 * Math.Asin(chordLength / (2 * radius))) * 180 / Math.PI;  //计算弦角度
            var startAngle = (Math.Atan2(this.startPoint.Y - center.Y, this.startPoint.X - center.X) * 180 / Math.PI);
            if (startAngle < 0)
            {
                startAngle += 360;
            }
            var endAngle = (startAngle + chordAngleRadians) % 360;
            if (startAngle < 0)
            {
                startAngle += 360;
            }
            if (endAngle < 0)
            {
                endAngle += 360;
            }
            endAngle = endAngle / 180 * Math.PI;
            startAngle = startAngle / 180 * Math.PI;
            Matrix3 matrix = Matrix3.GetTranslate(new Vector2(0, 0));
            Arc2d arc2D = new Arc2d();  //逻辑模型
            arc2D.IsClockwise = false;
            arc2D.StartAngle = startAngle;
            arc2D.EndAngle = endAngle;
            arc2D.Center = center;
            arc2D.Radius = radius;
            this.vportRt.DrawArc(arc2D, matrix, canvas, Constants.auxElementPen);
            return;
        }

        private void DrawArcByCenterAndAngle(SKCanvas canvas, Vector2 p0)
        {
            var startAngle = (Math.Atan2(this.startPoint.Y - center.Y, this.startPoint.X - center.X) * 180 / Math.PI);
            var endAngle = (Math.Atan2(p0.Y - center.Y, p0.X - center.X) * 180 / Math.PI);
            var radius = Vector2.Distance(center, this.startPoint);//计算半径
            endAngle = startAngle + endAngle; //端点角度
            if (endAngle > 360)
            {
                endAngle = endAngle - 360;
            }
            p0.X = center.X + radius * Math.Cos(endAngle);  //求端点
            p0.Y = center.Y + radius * Math.Sin(endAngle);
            endAngle = endAngle / 180 * Math.PI;
            startAngle = startAngle / 180 * Math.PI;
            Matrix3 matrix = Matrix3.GetTranslate(new Vector2(0, 0));
            Arc2d arc2D = new Arc2d();  //逻辑模型
            arc2D.IsClockwise = false;
            arc2D.StartAngle = startAngle;
            arc2D.EndAngle = endAngle;
            arc2D.Center = center;
            arc2D.Radius = radius;
            this.vportRt.DrawArc(arc2D, matrix, canvas, Constants.auxElementPen);
            return;
        }



        private void DrawArcByEndAndAngle(SKCanvas canvas, Vector2 p0)
        {
            double angle = (Math.Atan2(p0.Y-startPoint.Y, p0.X-startPoint.X) * 180 / Math.PI); //圆心旋转角度，弦长角度
            if (angle < 0)
            {
                angle += 360;
            }
            double length = Vector2.Distance(startPoint, midPoint);  //起点和终点之间的距离 也就是弦长
            var radius = (length / 2) / Math.Sin((angle / 2)*  Math.PI/180);        //半径
            center = new Vector2();
            center.X = startPoint.X + radius * Math.Sin((angle / 2)*Math.PI / 180); //圆心的x轴
            center.Y = startPoint.Y + radius * Math.Cos((angle / 2)* Math.PI / 180);
            if (angle > 180)
            {
                midPoint = startPoint;   //将开始点与端点取反
                startPoint = midPoint;
            }
            var startAngle = (Math.Atan2(startPoint.Y - center.Y, startPoint.X - center.X) * 180 / Math.PI);
            var endAngle = startAngle + angle;
            if (endAngle > 360)
            {
                endAngle -= 360;
            }
            startAngle = startAngle * Math.PI / 180;
            endAngle = endAngle * Math.PI / 180;
            Matrix3 matrix = Matrix3.GetTranslate(new Vector2(0, 0));
            Arc2d arc2D = new Arc2d();  //逻辑模型
            arc2D.IsClockwise = false;
            arc2D.StartAngle = startAngle;
            arc2D.EndAngle = endAngle;
            arc2D.Center = center;
            arc2D.Radius = radius;
            this.vportRt.DrawArc(arc2D, matrix, canvas, Constants.auxElementPen);
            return;
        }

        private void DrawAuxArc(SKCanvas canvas, Vector2 p0)
        {
            if (_methodName == "CreateARC")
            {
                DrawArcBy3P(canvas, p0);
            }
            if (_methodName == "PCE" || _methodName == "CPE")
            {
                DrawArcByCenterAnd2P(canvas, p0);
            }
            if (_methodName == "PCA"|| _methodName == "CPA")
            {
                DrawArcByCenterAndAngle(canvas, p0);
            }
            if (_methodName == "PCL" || _methodName == "CPL")
            {
                DrawArcByCenterAndLength(canvas, p0);
            }
            if(_methodName == "PEA")
            {
                DrawArcByEndAndAngle(canvas, p0);
            }
        }

        private void DrawAuxLine(SKCanvas canvas, Vector2 p0, Vector2 p1)
        {
            var sk_pre = this.vportRt.ConvertWcsToScr(p0).ToSKPoint();
            var sk_p = this.vportRt.ConvertWcsToScr(p1).ToSKPoint();
            //辅助元素的颜色 
            canvas.DrawLine(sk_pre, sk_p, Constants.auxElementPen);
            //辅助曲线的颜色，包括辅助长度，辅助角度等
        }

        public override void CreateElement(LcElement element, Matrix3 matrix)
        {
            var grp = element as LcGroup;
            foreach (var ele in grp.Elements)
            {
                var eleAction = (ele.RtAction as ElementAction);
                eleAction.CreateElement(ele, matrix);
            }
        }
        public override void CreateElement(LcElement element, Vector2 basePoint, double scaleFactor)
        {
            var grp = element as LcGroup;
            foreach (var ele in grp.Elements)
            {
                var eleAction = (ele.RtAction as ElementAction);
                eleAction.CreateElement(ele, basePoint, scaleFactor);
            }
        }
        public override void CreateElement(LcElement element, Vector2 basePoint, Vector2 scaleVector)
        {
            var grp = element as LcGroup;
            foreach (var ele in grp.Elements)
            {
                var eleAction = (ele.RtAction as ElementAction);
                eleAction.CreateElement(ele, basePoint, scaleVector);
            }
        }

        /// <summary>
        /// 获得圆弧的中点
        /// </summary>
        public Vector2 Getmidpoint(Arc2d arc2D)
        {

            var sage = arc2D.StartAngle * 180 / Math.PI;
            var eage = arc2D.EndAngle * 180 / Math.PI;
            double moveAngle = eage - sage;
            if (moveAngle < 0)
            {
                moveAngle += 360;
            }
            double midAngle = (moveAngle / 2);  //需要旋转的角度
            var matrix3 = Matrix3.Rotate(midAngle, center); //围绕圆心旋转
            var mp = matrix3.MultiplyPoint(this.startPoint); //旋转矩阵是逆时针旋转
            return new Vector2(mp.X, mp.Y);
            ////用到了起点，端点，圆心
            ////起点和终点相对于圆心的角度
            //double startAngle = (double)(Math.Atan2(startPoint.Y - center.Y, startPoint.X - center.X) * 180 / Math.PI); //将弧度转换为度数
            //double endAngle = (double)(Math.Atan2(endPoint.Y - center.Y, endPoint.X - center.X) * 180 / Math.PI);
            ////计算起点与终点的角度差  也就是移动角度
            //double moveAngle = endAngle - startAngle;

            //// 将角度差减半加上开始角度得到中点的角度
            //double midAngle = startAngle + (moveAngle / 2);
            ////计算半径
            //double radius = Vector2.Distance(startPoint, center);
            ////计算中点的坐标
            //midPoint = new Vector2();
            //midPoint.X = center.X + (double)(radius * Math.Cos(midAngle * Math.PI / 180));
            //midPoint.Y = center.Y + (double)(radius * Math.Sin(midAngle * Math.PI / 180));
            //return midPoint;
        }

        /// <summary>
        /// 获取两条线的夹角  
        /// </summary>
        /// <param name="line1start"></param>
        /// <param name="line1End"></param>
        /// <param name="line2start"></param>
        /// <param name="line2End"></param>
        /// <returns></returns>
        public double GetDegreesByTwoLine(Vector2 line1start, Vector2 line1End, Vector2 line2start, Vector2 line2End)
        {
            double x1 = line1start.X;
            double y1 = line1start.Y;
            double x2 = line1End.X;
            double y2 = line1End.Y;
            double x3 = line2start.X;
            double y3 = line2start.Y;
            double x4 = line2End.X;
            double y4 = line2End.Y;
            // 计算线段的向量表示
            double v1x = x2 - x1;
            double v1y = y2 - y1;
            double v2x = x4 - x3;
            double v2y = y4 - y3;
            // 计算向量的内积
            double dotProduct = v1x * v2x + v1y * v2y;
            // 计算向量的长度
            double magnitudeV1 = Math.Sqrt(v1x * v1x + v1y * v1y);
            double magnitudeV2 = Math.Sqrt(v2x * v2x + v2y * v2y);
            // 计算夹角余弦值
            double cosine = dotProduct / (magnitudeV1 * magnitudeV2);
            // 将夹角余弦值转换为角度
            double angleRadians = Math.Acos(cosine);
            double angleDegrees = angleRadians * 180 / Math.PI;
            return angleDegrees;
        }
        public bool IsPlugAng(Vector2 line1start, Vector2 line1End, Vector2 line2start, Vector2 line2End)
        {
            double x1 = line1start.X;
            double y1 = line1start.Y;
            double x2 = line1End.X;
            double y2 = line1End.Y;
            double x3 = line2start.X;
            double y3 = line2start.Y;
            double x4 = line2End.X;
            double y4 = line2End.Y;
            if (x1 == x2)
            {
                if (x4 < x1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
                if (y1 == y2)
            {
                if (y4 > y1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                double xa = x2 - x1;
                double ya = y2 - y1;
                double xb = x4 - x3;
                double yb = y4 - y3;
                double angle = xa * yb - xb * ya;
                if (angle > 0)
                { return true; }
                else
                { return false; }
            }
        }
        public int IsTopInLine(Vector2 line1start, Vector2 line1End, Vector2 point)
        {
            Vector2 S;
            Vector2 E;
            S = line1End;
            E = line1start;
            double Tmp = (S.Y - E.Y) * point.X + (E.X - S.X) * point.Y + S.X * E.Y - E.X * S.Y;
            if (Tmp == 0)
            {
                return 0;
            }
            if (Tmp > 0)
            {
                return 1;
            }
            else
            { return -1; }
        }
        public Vector2 GetVerticalpoint(Vector2 point, Vector2 center, Vector2 spoint, double ANG)
        {
            double x1 = point.X;
            double y1 = point.Y;
            double x2 = center.X;
            double y2 = center.Y;
            double x;
            double y;
            if (x1 == x2)
            {
                y = spoint.Y;
                x = x1;
            }
            else
            if (y1 == y2)
            {
                y = y1;
                x = spoint.X;
            }
            else
            {
                double k1 = (y2 - y1) / (x2 - x1);
                double b1 = y1 - k1 * x1;
                double k = -1 / k1;
                double b = spoint.Y - k * spoint.X;
                x = (b1 - b) / (k - k1);
                y = k * x + b;
            }

            if (ANG < 180)
            {
                Matrix3 matrix31 = Matrix3.GetMove(new Vector2(x, y), point);
                return matrix31.MultiplyPoint(spoint);
            }
            else
            {
                Matrix3 matrix31 = Matrix3.GetMove(spoint, point);
                return matrix31.MultiplyPoint(new Vector2(x, y));
            }

        }
        public override void DrawTemp(SKCanvas canvas)
        {
        }
        public override void Draw(SKCanvas canvas, LcElement element, Matrix3 matrix)
        {
        }

        public override ControlGrip[] GetControlGrips(LcElement element)
        {
            var group = element as LcArc;
            var grips = new List<ControlGrip>();
            var gripCenter = new ControlGrip
            {
                Element = group,
                Name = "Center",
                Position = group.Center
            };
            var gripStart = new ControlGrip
            {
                Element = group,
                Name = "Start",
                Position = group.Startp
            };
            var gripEnd = new ControlGrip
            {
                Element = group,
                Name = "End",
                Position = group.Endp
            };
            var gripArcMidp = new ControlGrip
            {
                Element = group,
                Name = "ArcMidp",
                Position = group.Midp
            };
            grips.Add(gripCenter);
            grips.Add(gripStart);
            grips.Add(gripEnd);
            grips.Add(gripArcMidp);
            return grips.ToArray();
        }
        private string _gripName;
        private Vector2 _position;
        private LcArc _group;

        public override void SetDragGrip(LcElement element, string gripName, Vector2 position, bool isEnd)
        {
            _group = element as LcArc;

            if (!isEnd)
            {
                _gripName = gripName;
                _position = position;
            }
            else
            {
                if (_gripName == "Start")
                {
                    _group.Set(start: _position);
                }
                if (_gripName == "End")
                {
                    _group.Set(end: _position);
                }
                if (_gripName == "ArcMidp")
                {
                    _group.Set(mid: _position);
                }
                if (_gripName == "Center")
                {
                    _group.Set(center: _position);
                }
                _group.LoadArcStatsProperty();
            }
        }

        public override void DrawDragGrip(SKCanvas canvas)
        {
            if (_group == null) return;
            LcArc lcarc = new LcArc();
            if (_gripName == "Start")
            {
                lcarc.Startp = _position;
                lcarc.Endp = _group.Endp;
                lcarc.Midp = _group.Midp;
            }
            if (_gripName == "End")
            {
                lcarc.Startp = _group.Startp;
                lcarc.Endp = _position;
                lcarc.Midp = _group.Midp;
            }
            if (_gripName == "ArcMidp")
            {
                lcarc.Startp = _group.Startp;
                lcarc.Endp = _group.Endp;
                lcarc.Midp = _position;
            }
            if (_gripName == "Center")
            {
                lcarc.Startp = _group.Startp + (_position - _group.Center);
                lcarc.Endp = _group.Endp + (_position - _group.Center);
                lcarc.Midp = _group.Midp + (_position - _group.Center);
            }
            lcarc.LoadArcStatsProperty();
            var center = this.vportRt.ConvertWcsToScr(lcarc.Center).ToSKPoint();
            var start = this.vportRt.ConvertWcsToScr(lcarc.Startp).ToSKPoint();
            var end = this.vportRt.ConvertWcsToScr(lcarc.Endp).ToSKPoint();
            double radius = Math.Sqrt(Math.Pow(center.X - end.X, 2) + Math.Pow(center.Y - end.Y, 2));
            float x1 = (float)(center.X - radius);
            float y1 = (float)(center.Y - radius);
            float x2 = (float)(center.X + radius);
            float y2 = (float)(center.Y + radius);
            SKRect skrect = new SKRect(x1, y1, x2, y2);
            var pen = this.GetDrawPen(_group);
            if (pen == Constants.defaultPen)
            {
                //TODO:这里可以考虑将实线用颜色做KEY，对SKPaint进行缓存
                using (var elePen = new SKPaint { Color = new SKColor(_group.GetColorValue()), IsStroke = true })
                {
                    canvas.DrawArc(skrect, (float)lcarc.StartAngle, (float)lcarc.MoveAngle, false, elePen);
                }
            }
            else
            {
                canvas.DrawArc(skrect, (float)lcarc.StartAngle, (float)lcarc.MoveAngle, false, pen);
            }
        }
        public override SnapPointResult SnapPoint(SnapRuntime snapRt, LcElement element, Vector2 point, bool forRef)
        {
            var maxDistance = vportRt.GetSnapMaxDistance();
            LcArc arc = element as LcArc;
            var sscur = SnapSettings.Current;
            var result = new SnapPointResult { Element = element };
            Arc2d arc2D = new Arc2d();
            arc2D.Center = arc.Center;
            arc2D.Radius = arc.Radius;
            arc2D.StartAngle = arc.StartAngle;
            arc2D.EndAngle = arc.EndAngle;
            var icir = arc2D;
            if (SnapSettings.Current.ObjectOn)
            {
                if (sscur.PointType.Has(SnapPointType.Center))
                {
                    if ((point - arc.Center).Length() <= maxDistance)
                    {
                        result.Point = arc.Center;
                        result.Name = "Center";
                        result.Curves.Add(new SnapRefCurve(SnapPointType.Center, icir));
                    }
                }
                if (sscur.PointType.Has(SnapPointType.Quadrant))
                {
                    foreach (var item in GetControlGrips(element))
                    {
                        if (item.Name != "Center")
                        {
                            if ((point - item.Position).Length() <= maxDistance)
                            {
                                result.Point = item.Position;
                                result.Name = item.Name;
                                result.Curves.Add(new SnapRefCurve(SnapPointType.Quadrant, icir));
                            }
                        }
                    }
                }
                if (!forRef && result.Point == null)
                {
                    var distance = Vector2.Distance(point, arc.Center);
                    if (sscur.PointType.Has(SnapPointType.Nearest))
                    {
                        if (Math.Abs(arc.Radius - distance) < maxDistance)
                        {
                            double[] x = new double[2];
                            double[] y = new double[2];
                            var distancex = Vector2.Distance(point, new Vector2(x[0], x[1]));
                            var distancey = Vector2.Distance(point, new Vector2(y[0], y[1]));
                            var dir = (point - arc.Center).Normalize();
                            var nearest = arc.Center + dir * arc.Radius;
                            result.Point = nearest;
                            result.Name = "Nearest";
                            result.Curves.Add(new SnapRefCurve(SnapPointType.Nearest, icir));
                        }
                    }
                }
            }

            if (result.Point != null)
                return result;
            else
                return null;
        }
        public override List<PropertyObserver> GetPropertyObservers()
        {
            return new List<PropertyObserver>()
            {
            };
        }
    }
}
